package com.monitor.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.monitor.model.Log;
import com.monitor.model.MonitorRecord;
import com.monitor.model.Record;
import com.monitor.model.Sensor;

public class IndexService {
	private static final Logger log = Logger.getLogger(IndexService.class);
	/**
	 * 得到平均温度
	 * @return int
 	 */
	public int getAverageTemperature(){
		String sql = "SELECT AVG(temperature) FROM record where dayofyear(recordTime) = DAYOFYEAR(now())";
		return Db.queryBigDecimal(sql) == null ? 0 :Db.queryBigDecimal(sql).intValue();
	}
	
	/**
	 * 得到平均湿度
	 * @return int 
 	 */
	public int getAverageHumidity(){
		String sql = "SELECT AVG(humidity) FROM record where dayofyear(recordTime) = DAYOFYEAR(now())";
		return Db.queryBigDecimal(sql) == null ? 0 :Db.queryBigDecimal(sql).intValue();
	}
	
	/**得到每个节点实时数据
	 * @return
	 */
	public List<MonitorRecord> getSensorRecord(){
	
		String sql1 = "(select * from record where sensorId  = '";
		String sql2 = "' order by id desc limit 1)";
		String sql3 = " union ";
		String sql = "";
		List<Sensor> sensors = Sensor.sensor.findAll();
		for (Sensor sensor : sensors) {
			sql += sql1+ String.valueOf(sensor.getInt("id"))+sql2 + sql3;
		}
		sql = sql.substring(0, sql.length() - 6);
		
		List<Record> records = Record.record.find(sql);
		List<MonitorRecord> monitorRecords = new ArrayList<MonitorRecord>();
		for (Record record : records) {
			MonitorRecord e = new MonitorRecord();
			e.setTemperature(record.getInt("temperature"));
			e.setHumidity(record.getInt("humidity"));
			e.setRecordTime(record.getDate("recordTime"));
			e.setName(Sensor.sensor.findById(record.getInt("sensorId")).getStr("name"));
			e.setSmoke(record.getInt("smoke"));
			e.setWarning(record.getInt("warning"));
			monitorRecords.add(e);
		}
	
		return monitorRecords;
		
	}
	
	/** 得到日温度记录和日湿度记录
	 * @return List<Rescord>
	 */
	public List<MonitorRecord> getDayReord(){
		List<MonitorRecord> monitorRecords = new ArrayList<MonitorRecord>();
		String sql = "SELECT round(avg(temperature),0) as temperature,round(avg(humidity),0) as humidity, recordTime "
				+ "from record  where date_format(recordTime,'%y-%m-%d') = date_format(now(),'%y-%m-%d') "
				+ "group by date_format(recordTime,'%H');";
		List<Record> records  = Record.record.find(sql);
		for (Record r : records) {
			MonitorRecord e = new MonitorRecord();
			e.setTemperature(r.getBigDecimal("temperature").intValue());
			e.setHumidity(r.getBigDecimal("humidity").intValue());
			e.setRecordTime(r.getDate("recordTime"));
			monitorRecords.add(e);
		}
		
		return monitorRecords;
		
	}
	
	/**
	 * 得到月记录
	 * @return  List<Rescord>
	 */
	public List<MonitorRecord> getMonthRecord(){
		List<MonitorRecord> monitorRecords = new ArrayList<MonitorRecord>();
		String sql = "SELECT avg(temperature) as temperature,avg(humidity) as humidity, recordTime "
				+ "from record  where date_format(recordTime,'%y-%m') = date_format(now(),'%y-%m') "
				+ "group by date_format(recordTime,'%d');";
		List<Record> records = Record.record.find(sql);
		for (Record r : records) {
			MonitorRecord e = new MonitorRecord();
			e.setTemperature(r.getBigDecimal("temperature").intValue());
			e.setHumidity(r.getBigDecimal("humidity").intValue());
			e.setRecordTime(r.getDate("recordTime"));
			monitorRecords.add(e);
		}
		return monitorRecords;
	}
	
	/**
	 * 得到日志信息
	 * @return  List<Log>
	 */
	public List<Log> getLogs(){
		Page<Log> logs = Log.me.paginate(1, 10, "select * ", "from log order by date desc");
		return logs.getList();
		
	}
	
	/**
	 * 得到今天的日志数量
	 * @return
	 */
	public int  getTodayLog(){
		String sql = "select count(*) from log where date_format(date,'%d') = date_format(now(),'%d')";
		return Db.queryLong(sql).intValue();
	}
}
